﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Shared.Method
{
    /// <summary>
    /// XML处理类，保存ORACLE配置
    /// </summary>
    public class ConfigDatabase
    {
        private static readonly string BASE_PATH = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        private const string XML_FILE = "Server.xml";
        private const string ROOT_NAME = "ServerSet";

        private const string TABLE_NAME = "TableName";//表节点
        private const string CONN_STR = "ConnStr";//连接字符串


        /// <summary>
        /// 设置表配置
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void SetXmlConfigTable(string name, string value)
        {
            CreateXML();
            XDocument document = XDocument.Load(BASE_PATH + XML_FILE);

            XElement root = document.Root;
            XElement table = root.Element(TABLE_NAME);

            table.SetElementValue(name, value);
            document.Save(BASE_PATH + XML_FILE);
        }
        /// <summary>
        /// 设置连接配置
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void SetXmlConfigConnStr(string name, string value)
        {
            CreateXML();
            XDocument document = XDocument.Load(BASE_PATH + XML_FILE);

            XElement root = document.Root;
            XElement connStr = root.Element(CONN_STR);

            connStr.SetElementValue(name, value);
            document.Save(BASE_PATH + XML_FILE);
        }

        private static string serverXmlStr = null;

        /// <summary>
        /// 获取连接节点下的内容
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetXmlConfigConnStr(string name)
        {
            //读取文件
            if (string.IsNullOrEmpty(serverXmlStr)&& File.Exists(BASE_PATH + XML_FILE))
            {
                StreamReader sr = new StreamReader(BASE_PATH + XML_FILE, Encoding.Default);
                serverXmlStr = sr.ReadToEnd();
            }

            if (string.IsNullOrEmpty(serverXmlStr) || string.IsNullOrEmpty(name))//文件不存在返回空
            {
                return null;
            }

            try
            {
                XDocument document = XDocument.Parse(serverXmlStr);
                XElement root = document.Root;
                XElement table = root.Element(CONN_STR);
                return table.Element(name).Value;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 获取table节点下的相应内容
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetXmlConfigTable(string name)
        {
            //读取文件
            if (string.IsNullOrEmpty(serverXmlStr) && File.Exists(BASE_PATH + XML_FILE))
            {
                StreamReader sr = new StreamReader(BASE_PATH + XML_FILE, Encoding.Default);
                serverXmlStr = sr.ReadToEnd();
            }

            if (string.IsNullOrEmpty(serverXmlStr) || string.IsNullOrEmpty(name))//文件不存在返回空
            {
                return null;
            }

            try
            {
                XDocument document = XDocument.Parse(serverXmlStr);
                XElement root = document.Root;
                XElement table = root.Element(TABLE_NAME);
                return table.Element(name).Value;
            }
            catch
            {
                return null;
            }
        }

        private static void CreateXML()
        {
            //判断文件是否存在，不存在则创建，并写入基础的XML内容
            if (!File.Exists(BASE_PATH + XML_FILE))
            {
                var file = File.Create(BASE_PATH + XML_FILE);
                file.Close();
                XDocument doc = new XDocument();
                XElement ele = new XElement(ROOT_NAME);
                XElement table = new XElement(TABLE_NAME);
                XElement connStr = new XElement(CONN_STR);

                ele.Add(table);
                ele.Add(connStr);

                doc.Add(ele);
                doc.Save(BASE_PATH + XML_FILE);
            }
        }
    }
}
